﻿#if UNITY_EDITOR
 using UnityEditor;
using UnityEngine;

namespace DL.UICheck.Editor
{
    public class UICheckToolsWindow : EditorWindow
    {
        private static int _newCanvasColorsLength;
        private static bool _isFirstOpen = true;

        [MenuItem("Window/UI Check Setting")]
        public static void ShowWindow()
        {
            _newCanvasColorsLength = UICheckTools.UICheckConfig.CanvasColors.Length;
            UICheckToolsWindow window = GetWindow<UICheckToolsWindow>(false, "UI Check Setting");
            if (_isFirstOpen)
            {
                window.minSize = new Vector2(400, 300);
                _isFirstOpen = false;
            }
        }
        
        void OnGUI()
        {
            GUILayout.Label("UICheck Settings", EditorStyles.boldLabel);

            Undo.RecordObject(UICheckTools.UICheckConfig, "UICheck enable");
            UICheckTools.UICheckConfig.Enbale =
                EditorGUILayout.Toggle("UICheck enable", UICheckTools.UICheckConfig.Enbale);

            Undo.RecordObject(UICheckTools.UICheckConfig, "UICheck onlyActive");
            UICheckTools.UICheckConfig.OnlyActive =
                EditorGUILayout.Toggle("UICheck onlyActive", UICheckTools.UICheckConfig.OnlyActive);
            
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Canvas Nesting levels");
            _newCanvasColorsLength = EditorGUILayout.IntField(_newCanvasColorsLength);
            if (GUILayout.Button("Change"))
            {
                Undo.RecordObject(UICheckTools.UICheckConfig, "Canvas Nesting levels");
                UICheckTools.SetCanvasColorsLength(_newCanvasColorsLength);
            }

            EditorGUILayout.EndHorizontal();

            // Canvas Colors
            GUILayout.Label("Canvas Colors:");
            for (int i = 0; i < UICheckTools.UICheckConfig.CanvasColors.Length; i++)
            {
                Undo.RecordObject(UICheckTools.UICheckConfig, "Canvas Colors");
                UICheckTools.UICheckConfig.CanvasColors[i] =
                    EditorGUILayout.ColorField($"Layer {i} Color", UICheckTools.UICheckConfig.CanvasColors[i]);
            }

            // UI Element Color
            Undo.RecordObject(UICheckTools.UICheckConfig, "UI Element Color");
            UICheckTools.UICheckConfig.UIElementColor =
                EditorGUILayout.ColorField("UI Element Color", UICheckTools.UICheckConfig.UIElementColor);

            // Thickness
            Undo.RecordObject(UICheckTools.UICheckConfig, "Thickness");
            UICheckTools.UICheckConfig.Thickness =
                EditorGUILayout.IntField("Thickness", UICheckTools.UICheckConfig.Thickness);

            // Dotted Line
            Undo.RecordObject(UICheckTools.UICheckConfig, "Dotted Line");
            UICheckTools.UICheckConfig.DottedLine =
                EditorGUILayout.Toggle("Dotted Line", UICheckTools.UICheckConfig.DottedLine);

            // Info
            Undo.RecordObject(UICheckTools.UICheckConfig, "Show Info");
            UICheckTools.UICheckConfig.Info = EditorGUILayout.Toggle("Show Info", UICheckTools.UICheckConfig.Info);

            // Overflow Color
            Undo.RecordObject(UICheckTools.UICheckConfig, "Overflow Color");
            UICheckTools.UICheckConfig.OverflowColor =
                EditorGUILayout.ColorField("Overflow Color", UICheckTools.UICheckConfig.OverflowColor);

            // Overflow Threshold
            Undo.RecordObject(UICheckTools.UICheckConfig, "Overflow Threshold");
            UICheckTools.UICheckConfig.OverflowThreshold =
                EditorGUILayout.FloatField("Overflow Threshold", UICheckTools.UICheckConfig.OverflowThreshold);

            // Check Precision
            Undo.RecordObject(UICheckTools.UICheckConfig, "Check Precision");
            UICheckTools.UICheckConfig.CheckPrecision =
                EditorGUILayout.IntField("Check Precision", UICheckTools.UICheckConfig.CheckPrecision);

            if (GUILayout.Button("ReSet Setting"))
            {
                Undo.RecordObject(UICheckTools.UICheckConfig, "ReSet Setting");
                UICheckTools.Reset();
                _newCanvasColorsLength = UICheckTools.UICheckConfig.CanvasColors.Length;
            }

            // Apply changes
            if (GUI.changed)
            {
                SceneView.RepaintAll();
            }
        }
        
        private void OnDisable()
        {
            UICheckTools.SaveConfig();
        }
    }
}
#endif

